package com.ldy.core.web;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ldy.core.common.Result;
import com.ldy.core.entity.BaseEntity;
import com.ldy.core.mapper.BaseEntityMapper;
import com.ldy.core.util.DatabaseUtil;
import com.ldy.core.vo.TableVO;

/**
 * 数据库设计
 */
@RestController
@RequestMapping("/db")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
public class DatabaseController {

	@Autowired
	private DatabaseUtil databaseUtil;

	/**
	 * 查询数据库表
	 * 
	 * @return
	 */
	@PostMapping("/selectList")
	public Result selectList() {
		return Result.success(databaseUtil.getTables());
	}

	/**
	 * 获取数据库字段信息
	 * 
	 * @param tableName
	 * @return
	 */
	@PostMapping("/get")
	public Result get(String tableName) {
		return Result.success(databaseUtil.getFields(tableName));
	}

	/**
	 * 创建表
	 * 
	 * @param code
	 * @param value
	 * @return
	 */
	@PostMapping("/table/create")
	public Result createTable(@RequestBody TableVO vo) {
		Assert.notNull(vo, "vo  is null");
		Assert.hasText(vo.getName(), "tableName  is null");
		databaseUtil.createTable(vo);
		return Result.success();
	}

	/**
	 * 更新表
	 * 
	 * @param code
	 * @param value
	 * @return
	 */
	@PostMapping("/table/update")
	public Result updateTable(@RequestBody TableVO vo) {
		Assert.notNull(vo, "vo  is null");
		Assert.hasText(vo.getId(), "ID  is null");
		Assert.hasText(vo.getName(), "tableName  is null");
		databaseUtil.updateTable(vo);
		return Result.success();
	}

	/**
	 * 删除表
	 * 
	 * @param code
	 * @param value
	 * @return
	 */
	@PostMapping("/table/del")
	public Result delTable(String tableName) {
		Assert.hasText(tableName, "tableName  is null");
		databaseUtil.deleteTable(tableName);
		return Result.success();
	}

	/**
	 * 表查询
	 * 
	 * @param code
	 * @param value
	 * @return
	 */
	@PostMapping("/table/query")
	public Result queryTable(String sql) {
		Assert.hasText(sql, "sql  is null");
		return Result.success(databaseUtil.executeSqlQuery(sql));
	}

	@Autowired
	private BaseEntityMapper mapper;

	/**
	 * 表分页查询
	 * 
	 * @return
	 */
	@PostMapping("/table/page")
	public Result queryPageTable(int current, int size, String tableName, String name, String value) {
		Assert.hasText(tableName, "tableName  is null");
		Assert.hasText(name, "name  is null");
		IPage<BaseEntity> page = new Page<BaseEntity>(current, size);
		List<Map<String, Object>> list = mapper.selectList(page, tableName, name, value);
		return Result.success().map("list", list).map("total", page.getTotal());
	}
}
